using B.S.RbacData.ErrorCode;
using B.S.RbacData.Infrastructure;
using B.S.RbacData.Read.API.Applications.Commands.Traceabilitys;
using B.S.RbacData.Read.API.Dtos.Traceabilitys;
using MediatR;
using Microsoft.EntityFrameworkCore;
using System.Security.Claims;

namespace B.S.RbacData.Read.API.Applications.CommandHandlters.Traceabilitys
{
    /// <summary>
    /// 根据ID获取产品信息命令处理器
    /// </summary>
    public class GetProductByIdCommandHandlter : IRequestHandler<GetProductByIdCommand, ApiResult<GetProductDtos>>
    {
        private readonly DataDbcontext _dbContext;
        private readonly ILogger<GetProductByIdCommandHandlter> _logger;
        private readonly IHttpContextAccessor contextAccessor;

        /// <summary>
        /// 构造函数
        /// </summary>
        public GetProductByIdCommandHandlter(
            DataDbcontext dbContext,
            ILogger<GetProductByIdCommandHandlter> logger,IHttpContextAccessor contextAccessor)
        {
            _dbContext = dbContext;
            _logger = logger;
            this.contextAccessor = contextAccessor;
        }

        /// <summary>
        /// 处理获取产品信息命令
        /// </summary>
        public async Task<ApiResult<GetProductDtos>> Handle(GetProductByIdCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var userName = contextAccessor.HttpContext.User.FindFirstValue("UserName");


                var product = await _dbContext.Products
                    .Include(p => p.Goods)
                    .Where(p => p.ID == request.Id && !p.IsDeleted)
                    .Select(p => new GetProductDtos
                    {
                        ID = p.ID,
                        GoodsId = p.GoodsId,
                        ProductionBatch = p.ProductionBatch,
                        ShelfLifeDay = p.ShelfLifeDay,
                        ProductionLicenseNumber = p.ProductionLicenseNumber,
                        Manufacturer = p.Manufacturer,
                        CoverImage = p.CoverImage,
                        Completeness = p.Completeness,
                        CreateUserName = userName,
                        UpdateUserName = userName,
                        CreateTime = p.CreateTime,
                        GoodsName = p.Goods.GoodsName
                    })
                    .FirstOrDefaultAsync(cancellationToken);

                if (product == null)
                {
                    return new ApiResult<GetProductDtos>
                    {
                        code = ResultEnumCode.NotFind,
                        message = "产品不存在",
                        data = null
                    };
                }

                return new ApiResult<GetProductDtos>
                {
                    code = ResultEnumCode.Ok,
                    message = "获取成功",
                    data = product
                };
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取产品信息时发生错误");
                return new ApiResult<GetProductDtos>
                {
                    code = ResultEnumCode.fails,
                    message = "获取产品信息时发生错误：" + ex.Message,
                    data = null
                };
            }
        }
    }
} 